package cn.zjj.sevice.impl;

import cn.zjj.domain.MovieTicket;
import cn.zjj.domain.User;
import cn.zjj.dto.UserTicketDto;
import cn.zjj.enums.GlobalErrorCode;
import cn.zjj.exception.GlobalCustomException;
import cn.zjj.feign.MovieFeignClient;
import cn.zjj.mapper.UserMapper;
import cn.zjj.result.JsonResult;
import cn.zjj.sevice.IUserService;
import cn.zjj.utils.AssertUtil;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.Random;

@Service
public class UsersSeviceImpl implements IUserService {
    @Autowired
    private MovieFeignClient movieFeignClient;

    @Autowired
    private UserMapper userMapper;

    //观影
    @Override
    public MovieTicket seeMovie(UserTicketDto userTicketDto) {
        JsonResult jsonResult = movieFeignClient.selectUserTicket(userTicketDto);
        if(!jsonResult.isSuccess()){
            throw new GlobalCustomException(jsonResult.getMessage(),jsonResult.getCode());
        }
        Object data = jsonResult.getData();
        Map<String,Object> map=(Map<String,Object>)data;
        MovieTicket movieTicket = JSONObject.parseObject(JSONObject.toJSONString(map), MovieTicket.class);
        return movieTicket;
    }
    //所有票
    @Override
    public List<MovieTicket> selectAllTicket() {
        JsonResult select = movieFeignClient.select();
        if(!select.isSuccess()){
            throw new GlobalCustomException(select.getMessage(),select.getCode());
        }
        Object data = select.getData();
        List<MovieTicket> movieTickets=(List<MovieTicket>)data;

        return movieTickets;
    }

    //购票
    @Override
    public void buyTicket(UserTicketDto buyTicketDto) {
        JsonResult jsonResult = movieFeignClient.buyTicket(buyTicketDto);
        if(!jsonResult.isSuccess()){
            throw new GlobalCustomException(jsonResult.getMessage(),jsonResult.getCode());
        }
        User user = userMapper.selectById(buyTicketDto.getUser_id());
        BigDecimal balance = user.getBalance();
        MovieTicket movieTicket = selectByTicketId(buyTicketDto.getMovie_ticket_id());
        BigDecimal price = movieTicket.getPrice();
        user.setBalance(balance.subtract(price));
        userMapper.update(user);
    }

    //根据票id查
    @Override
    public MovieTicket selectByTicketId(Long id) {
        JsonResult jsonResult = movieFeignClient.selectById(id);
        if(!jsonResult.isSuccess()){
            throw new GlobalCustomException(jsonResult.getMessage(),jsonResult.getCode());
        }
        Object data = jsonResult.getData();
        Map<String,Object> map=(Map<String,Object>)data;
        MovieTicket movieTicket = JSONObject.parseObject(JSONObject.toJSONString(map), MovieTicket.class);
        return movieTicket;
    }

    @Override
    public void save(User user) {
        userMapper.insert(user);
        JsonResult jsonResult = movieFeignClient.getAllId();
        Object data = jsonResult.getData();
        List<Integer> ids=(List<Integer>)data;
        //集合随机元素
        Random random = new Random();
        Integer id = ids.get(random.nextInt(ids.size()));
        //转为long
        Long movie_ticket_id = Long.valueOf(id);
        UserTicketDto userTicketDto = new UserTicketDto();
        userTicketDto.setUser_id(user.getId());
        userTicketDto.setMovie_ticket_id(movie_ticket_id);
        //调用购票接口
        movieFeignClient.buyTicket(userTicketDto);
    }
}
